package com.ict.thread;

import com.ict.common.FileConfigGet;
import com.ict.common.LogUtils;
import com.ict.common.TimeUtils;
import com.ict.start.AisPos;
import com.ict.start.Lvdiantu;


/**
 * Created by chenlong on 2017/10/9.
 * Info:
 */
public class LvdiantuSynThread implements Runnable {
    private AisPos aisPos;
    private FileConfigGet fileConfigGet;
    private String proFileRoad;
    private String copyStr;
    private String FILEPATH;

    public LvdiantuSynThread(AisPos aisPos,FileConfigGet fileConfigGet){
        this.fileConfigGet=fileConfigGet;
        this.aisPos=aisPos;
        proFileRoad=fileConfigGet.getFileRoad();
        System.out.println(proFileRoad);
        FILEPATH=fileConfigGet.getFileRoad()+"\\logs\\GreenGraph_DataClear.log";
        System.out.println(FILEPATH);
    }

    @Override
    public void run() {
            while (true){
                try{
                    //更新生成绿点图数据文件
                    aisPos.init();
                    aisPos.updateAisPos();

                    //清理过期数据
//                    aisPos.clearStaleData();
                    aisPos.close();

                    copyStr="copy "+proFileRoad+"\\"+fileConfigGet.getFileName()+" "+proFileRoad+"\\"+fileConfigGet.getNewFsName();
//                    System.out.println(TimeUtils.getNowTime()+ " : Data copy start");
                    LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Data copy start");

                    boolean ifSuc1=Lvdiantu.excuteCmd(copyStr);
                    if (ifSuc1){
//                        System.out.println(TimeUtils.getNowTime()+ " : Data copy success");
                        LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Data copy success");
                    }else {
//                        System.out.println(TimeUtils.getNowTime()+ " : Data copy Failed");
                        LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Data copy Failed");
                    }

                    //启动绿点图生成图片程序
//                    System.out.println(TimeUtils.getNowTime()+ " : Lvdiantu get start......");
                    LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Lvdiantu get start......");
                    boolean ifSuc=Lvdiantu.excuteCmd(proFileRoad+"\\lvdiantu\\Distribution.exe");
                    if (ifSuc){
//                        System.out.println(TimeUtils.getNowTime()+ " : Lvdiantu get success");
                        LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Lvdiantu get success");
                    }else {
//                        System.out.println(TimeUtils.getNowTime()+ " : Lvdiantu get Failed");
                        LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Lvdiantu get start......");
                    }

                    //定时任务，指定时间段以后执行下面任务
                    Thread.sleep(1000*60*Integer.parseInt(fileConfigGet.getWkTime()));

                    //删除绿点图文件目录
//                    System.out.println(TimeUtils.getNowTime()+ " : Lvdiantu clear start......");
                    LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Lvdiantu clear start......");
                    Lvdiantu.excuteCmd("rd /s/q "+fileConfigGet.getGgRoad());
                    Lvdiantu.excuteCmd("rd /s/q "+fileConfigGet.getChartRoad());
//                    System.out.println(TimeUtils.getNowTime()+ " : Lvdiantu clear success");
                    LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Lvdiantu clear success");
                }catch (Exception e){
//                    System.out.println(TimeUtils.getNowTime()+ " : Thread run error");
                    LogUtils.LogWriteAppend(FILEPATH,TimeUtils.getNowTime()+ " : Thread run error");
                    e.printStackTrace();
                }
        }
    }
}